package com.fastCms.business.blueorder.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fastCms.business.blueorder.daos.BlueMaterialMapper;
import com.fastCms.business.blueorder.pojo.entity.BlueMaterial;
import com.fastCms.business.blueorder.pojo.po.BlueMaterialPO;
import com.fastCms.business.blueorder.pojo.po.BlueMaterialSavePO;
import com.fastCms.business.blueorder.pojo.vo.BlueDeviceVO;
import com.fastCms.business.blueorder.pojo.vo.BlueMaterialVO;
import com.fastCms.business.blueorder.service.BlueMaterialService;
import com.fastCms.business.frame.constant.CommonConst;
import com.fastCms.business.frame.exception.BusinessException;
import com.fastCms.business.frame.manage.CloudUserManage;
import com.fastCms.business.frame.manage.UserManage;
import com.fastCms.business.frame.pojo.PageParam;
import com.fastCms.business.frame.pojo.po.IdStrPO;
import com.fastCms.business.frame.response.ApiResponse;
import com.fastCms.business.frame.response.ResponseUtil;
import com.fastCms.business.frame.utils.CommonsUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * blue-材料相关 服务实现类
 * </p>
 *
 * @author quan
 * @since 2024-10-17
 */
@Service
public class BlueMaterialServiceImpl extends ServiceImpl<BlueMaterialMapper, BlueMaterial> implements BlueMaterialService {

    @Autowired
    private BlueMaterialMapper blueMaterialMapper;

    @Override
    public PageInfo<BlueMaterialVO> showPage(PageParam<BlueMaterialPO> pageParam) {

        String organizeCode = CloudUserManage.getOrganizeCode();
        BlueMaterialPO blueMaterialPO = pageParam.getQueryFields();
        blueMaterialPO.setOrganizeCode(organizeCode);
        PageInfo<BlueMaterialVO> result = PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize())
                .doSelectPageInfo(() ->
                        blueMaterialMapper.showList(blueMaterialPO));
        return result;
    }

    @Override
    public Boolean saveOrUpdateMaterial(BlueMaterialSavePO param) throws BusinessException {
        if (StringUtils.isBlank(param.getId())) {
            BlueMaterial blueMaterial = new BlueMaterial();
            CommonsUtil.initEntity(blueMaterial);
            BeanUtil.copyProperties(param, blueMaterial);
            blueMaterial.setOrganizeCode(CloudUserManage.getOrganizeCode());
            this.save(blueMaterial);
        } else {
            BlueMaterial blueMaterial = getById(param.getId());
            if (blueMaterial == null) {
                throw new BusinessException("数据不存在");
            }
            BeanUtil.copyProperties(param, blueMaterial);
            this.updateById(blueMaterial);
        }
        return true;
    }

    @Override
    public ApiResponse<Boolean> delete(IdStrPO param) throws BusinessException {
        BlueMaterial blueMaterial = getById(param.getId());
        if (blueMaterial == null) {
            throw new BusinessException("数据不存在");
        }
        blueMaterial.setState(CommonConst.STATE.INVALID);
        this.updateById(blueMaterial);
        return ResponseUtil.success(true);
    }


}
